package com.jsd2412.backend.employee.service.impl;

import com.jsd2412.backend.base.exception.ServiceException;
import com.jsd2412.backend.base.response.StatusCode;
import com.jsd2412.backend.employee.mapper.EmployeeMapper;
import com.jsd2412.backend.employee.pojo.dto.EmployeeLoginParam;
import com.jsd2412.backend.employee.pojo.vo.EmployeeVO;
import com.jsd2412.backend.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public EmployeeVO login(EmployeeLoginParam employeeLoginParam, HttpSession session) {
        EmployeeVO employeeVO = employeeMapper.selectByUsernameAndPassword(employeeLoginParam);
        if (employeeVO != null) {
            //暂存员工标识
            session.setAttribute("employee", employeeVO);
            return employeeVO;
        } else {
            throw new ServiceException(StatusCode.USERNAME_PASSWORD_ERROR);
        }

    }

    @Override
    public EmployeeVO currentEmployee(HttpSession session) {
        return (EmployeeVO) session.getAttribute("employee");
    }

    @Override
    public void logout(HttpSession session) {
        session.removeAttribute("employee");//删除会话标识
    }


}
